/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.babble.subitem.controller;

import com.babble.subitem.dto.SubjectSaveDTO;
import com.babble.subitem.dto.SubjectUpdDTO;
import com.babble.subitem.entity.Subject;
import com.babble.subitem.service.ISubjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springblade.common.enums.CodeMsg;
import org.springblade.common.exception.MsgException;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.tool.api.R;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 专题表 控制器
 *
 * @author Blade
 * @since 2021-11-21
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/subject")
@Api(value = "专题表", tags = "专题表接口")
public class SubjectController extends BladeController {
	private final ISubjectService subjectService;
	@ApiOperation(value = "获取分页专题")
	@GetMapping("list")
	public R<List<Subject>> selectSubjects(@RequestParam(required = false) Map<String,String> map) throws MsgException {
		List<Subject> tags = subjectService.selectSubjects(map);
		return R.data(tags);
	}
	@ApiOperation(value = "点击专题")
	@PostMapping("click")
	public R clickSubject(@RequestParam("id") String id) throws MsgException, MsgException {
		subjectService.clickSubject(id);
		return R.data("点击成功");
	}
	@ApiOperation(value = "根据ID获取")
	@GetMapping("getSubjcetIsId")
	public R<Subject> getSubjcetIsId(@RequestParam("id") String id) throws MsgException {
		List<Subject> tags = subjectService.getSubjcetIsId(id);
		if(tags==null || tags.size()==0){
			throw new MsgException(CodeMsg.SUBJCET_EXISTS_ERROR.getCode(),CodeMsg.SUBJCET_EXISTS_ERROR.getMsg());
		}
		return R.data(tags.get(0));
	}
	@PreAuth("hasRole('administrator')")
	@ApiOperation(value = "添加专题")
	@PostMapping("save")
	public R save(@Valid @RequestBody SubjectSaveDTO subjectSaveDTO) throws MsgException, MsgException, MsgException {
		subjectService.saveToSubject(subjectSaveDTO);
		return R.data("添加成功");
	}
	@PreAuth("hasRole('administrator')")
	@ApiOperation(value = "修改专栏")
	@PostMapping("upd")
	public R upd(@Valid @RequestBody SubjectUpdDTO subjectUpdDTO) throws MsgException, MsgException {
		subjectService.updToSubject(subjectUpdDTO);
		return R.data("修改成功");
	}
}
